<?php

namespace app\common\model\banxi\learn;

use app\common\enums\LearnMeansEnum;
use app\common\model\banxi\say\SayTeachSentence;
use app\common\model\banxi\student\Student;
use app\common\model\banxi\trainer\Trainer;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 全科做题记录
 */
class LearnSayQuestionRecord extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_learn_say_question_record';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'useTimeDate',
        'learnMeansCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getUseTimeDateAttr($value, $data)
    {
        $value = $value ?: ($data['useTime'] ?? '');
        return secondToTime($value);
    }

    public function getLearnMeansCnAttr($value, $data)
    {
        $value = $value ?: ($data['learnMeans'] ?? '');
        return (new LearnMeansEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------
    public function geQuestionRecordStatistics($skRecordId)
    {
        $list = $this->where([
            'skRecordId' => $skRecordId,
            'isSubmit' => 1,
            'learnMeans' => LearnMeansEnum::E1
        ])->select()->toArray();
        $sayTeachSentenceIds = array_column($list, 'sayTeachSentenceId');
        $sayTeachSentenceLists = SayTeachSentence::alias('a')
            ->join('banxi_say_grade b', 'b.id = a.gradeId')
            ->where([['a.id', 'in', $sayTeachSentenceIds]])
            ->column('b.name,a.name as sentenceName', 'a.id');
        //学习句子
        $studyCourseIds = [];
        $translationLearnNum = 0;
        $translationErrorNum = 0;
        $answerLearnNum = 0;
        $answerErrorNum = 0;
        $learnSentenceCount = 0;
        $learnWordCount = 0;
        $sentenceGradeName = [];
        foreach ($list as $item) {
            if (!in_array($item['sayTeachSentenceId'], $studyCourseIds)) {
                $studyCourseIds[] = $item['sayTeachSentenceId'];
            }
            $translationLearnNum += $item['translationLearnNum'];
            $translationErrorNum += $item['translationErrorNum'];
            $answerLearnNum += $item['answerLearnNum'];
            $answerErrorNum += $item['answerErrorNum'];
            $learnSentenceCount += $item['learnSentenceCount'];
            $learnWordCount += $item['learnWordCount'];
            $teachSentenceInfo = $sayTeachSentenceLists[$item['sayTeachSentenceId']] ?? [];
            if (isset($teachSentenceInfo['sentenceName'], $teachSentenceInfo['name'])){
                $sentenceGradeName[] =  "{$teachSentenceInfo['name']}【{$teachSentenceInfo['sentenceName']}】({$item['learnSentenceCount']})";
            }
        }
        $sentenceGradeName = implode('/', $sentenceGradeName);
        $totalNum = count($studyCourseIds);
        $translationRightRate = $translationLearnNum > 0 ? intval($translationErrorNum * 100 / $translationLearnNum) : 0;
        $answerRightRate = $answerLearnNum > 0 ? intval($answerErrorNum * 100 / $answerLearnNum) : 0;
        return [
            'sentenceGradeName' => $sentenceGradeName,
            'totalNum' => $totalNum,
            'translationLearnNum' => $translationLearnNum,
            'translationErrorNum' => $translationErrorNum,
            'translationRightRate' => $translationRightRate,
            'answerLearnNum' => $answerLearnNum,
            'answerErrorNum' => $answerErrorNum,
            'answerRightRate' => $answerRightRate,
            'learnSentenceCount' => $learnSentenceCount,
            'learnWordCount' => $learnWordCount
        ];
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function student()
    {
        return $this->hasOne(Student::class, 'id', 'studentId');
    }

    public function trainer()
    {
        return $this->hasOne(Trainer::class, 'id', 'trainerId');
    }

    public function questionAnswerRecordList()
    {
        return $this->hasMany(LearnSayQuestionAnswerRecord::class, 'recordId', 'id');
    }

    public function sayTeachSentenceInfo()
    {
        return $this->hasOne(SayTeachSentence::class, 'id', 'sayTeachSentenceId');
    }

    public function skRecordInfo()
    {
        return $this->hasOne(LearnSkRecord::class, 'id', 'skRecordId');
    }
}